home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtoyc01.zip / HELP.CPP < prev    next >
C/C++ Source or Header  |  1994-01-07  |  10KB  |  357 lines

  1. /*------------------------------------------------------------*/
  2. /* filename -       Help.cpp                                  */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  Member function(s) of following classes   */
  6. /*                      THelpViewer                           */
  7. /*                      THelpWindow                           */
  8. /*------------------------------------------------------------*/
  9.  
  10. /*------------------------------------------------------------*/
  11. /*                                                            */
  12. /*    Turbo Vision -  Version 1.0                             */
  13. /*                                                            */
  14. /*                                                            */
  15. /*    Copyright (c) 1991 by Borland International             */
  16. /*    All Rights Reserved.                                    */
  17. /*                                                            */
  18. /*------------------------------------------------------------*/
  19.  
  20. #define Uses_TStreamableClass
  21. #define Uses_TPoint
  22. #define Uses_TStreamable
  23. #define Uses_ipstream
  24. #define Uses_opstream
  25. #define Uses_fpstream
  26. #define Uses_TRect
  27. #define Uses_TScrollBar
  28. #define Uses_TScroller
  29. #define Uses_TDrawBuffer
  30. #define Uses_TEvent
  31. #define Uses_TWindow
  32. #define Uses_TKeys
  33. #define Uses_TPalette
  34. #include <tv.h>
  35.  
  36. #if !defined( __HELP_H )
  37. #include "Help.h"
  38. #endif  // __HELP_H
  39.  
  40. #if !defined( __UTIL_H )
  41. #include "Util.h"
  42. #endif  // __UTIL_H
  43.  
  44. #if !defined( __STRING_H )
  45. #include <string.h>
  46. #endif  // __STRING_H
  47.  
  48. #if !defined( __LIMITS_H )
  49. #include <limits.h>
  50. #endif  // __LIMITS_H
  51.  
  52. #if !defined( __STAT_H )
  53. #include <sys\stat.h>
  54. #endif  // __STAT_H
  55.  
  56. #if !defined( __CTYPE_H )
  57. #include <ctype.h>
  58. #endif  // __CTYPE_H
  59.  
  60. #if !defined( __IO_H )
  61. #include <io.h>
  62. #endif  // __IO_H
  63.  
  64. #pragma warn -dsz
  65.  
  66. // THelpViewer
  67.  
  68.  
  69. #ifdef HELPEXTENSIONS
  70.     int THelpViewer::oldSelected[THelpFile::maxOldTopics+1];
  71. #endif
  72.  
  73.  
  74. THelpViewer::THelpViewer( const TRect& bounds, TScrollBar* aHScrollBar,
  75.     TScrollBar* aVScrollBar, THelpFile *aHelpFile, ushort context )
  76.     : TScroller( bounds, aHScrollBar, aVScrollBar )
  77. {
  78.     options = (options | ofSelectable);
  79.     growMode = gfGrowHiX | gfGrowHiY;
  80.     hFile = aHelpFile;
  81.     topic = aHelpFile->getTopic(context);
  82.     topic->setWidth(size.x);
  83.     setLimit(78, topic->numLines());
  84.     selected = 1;
  85. }
  86.  
  87. THelpViewer::~THelpViewer()
  88. {
  89.     delete hFile;
  90.     delete topic;
  91. }
  92.  
  93. void THelpViewer::changeBounds( const TRect& bounds )   // fix Help-3
  94. {
  95.     TScroller::changeBounds(bounds);
  96.     topic->setWidth(size.x);
  97.     setLimit(limit.x, topic->numLines());
  98. }
  99.  
  100. void THelpViewer::draw()
  101. {
  102.     TDrawBuffer b;
  103.     char line[256];
  104.     char buffer[256];
  105.     char *bufPtr;
  106.     int i, j, l;
  107.     int keyCount;
  108.     ushort normal, keyword, selKeyword, c;
  109.     TPoint keyPoint;
  110.     uchar keyLength;
  111.     int keyRef;
  112.  
  113.     normal = getColor(1);
  114.     keyword = getColor(2);
  115.     selKeyword = getColor(3);
  116.     keyCount = 0;
  117.     keyPoint.x = 0;
  118.     keyPoint.y = 0;
  119.     topic->setWidth(size.x);
  120.     if (topic->getNumCrossRefs() > 0)
  121.         {
  122.         do
  123.             {                   // fix Help-2
  124.             topic->getCrossRef(keyCount, keyPoint, keyLength, keyRef);
  125.             ++keyCount;
  126.             } while ( (keyCount < topic->getNumCrossRefs()) && (keyPoint.y <= delta.y));
  127. /************************************
  128.             {
  129.             ++keyCount;
  130.             topic->getCrossRef(keyCount-1, keyPoint, keyLength, keyRef);
  131.             } while ( (keyCount <= topic->getNumCrossRefs()) && (keyPoint.y < delta.y));
  132. *************************************/
  133.         }
  134.     for (i = 1; i <= size.y; ++i)
  135.         {
  136.         b.moveChar(0, ' ', normal, size.x);
  137.         strcpy(line, topic->getLine(i + delta.y));
  138.         if (strlen(line) > delta.x)
  139.             {
  140.             bufPtr = line + delta.x; 
  141.             strncpy(buffer, bufPtr, size.x);
  142.             buffer[size.x] = 0;
  143.             b.moveStr(0, buffer, normal);
  144.             }
  145.         else
  146.             b.moveStr(0, "", normal);
  147.         while (i + delta.y == keyPoint.y)
  148.             {
  149.             l = keyLength;
  150.             if (keyPoint.x < delta.x )
  151.                 {
  152.                 l -= (delta.x - keyPoint.x);
  153.                 keyPoint.x = delta.x;
  154.                 }
  155.             if (keyCount == selected)
  156.                 c = selKeyword;
  157.             else
  158.                 c = keyword;
  159.             for(j = 0; j < l; ++j)
  160.                 b.putAttribute((keyPoint.x - delta.x + j),c);
  161. /***********************************************
  162.             ++keyCount;
  163.             if (keyCount <= topic->getNumCrossRefs())
  164.                 topic->getCrossRef(keyCount-1, keyPoint, keyLength, keyRef);
  165.             else
  166. ************************************************/
  167.             if ( keyCount < topic->getNumCrossRefs() )           // fix Help-2
  168.                {
  169.                topic->getCrossRef(keyCount, keyPoint, keyLength, keyRef);
  170.                ++keyCount;
  171.                }
  172.             else
  173.                 keyPoint.y = 0;
  174.             }
  175.         writeLine(0, i-1, size.x, 1, b);
  176.         }
  177. }
  178.  
  179. TPalette& THelpViewer::getPalette() const
  180. {
  181.     static TPalette palette(cHelpViewer, sizeof( cHelpViewer)-1);
  182.     return palette;
  183. }
  184.  
  185. void THelpViewer::makeSelectVisible( int selected, TPoint& keyPoint,
  186.          uchar& keyLength, int& keyRef )
  187. {
  188.     TPoint d;
  189.  
  190.     topic->getCrossRef(selected, keyPoint, keyLength, keyRef);
  191.     d = delta;
  192.     if (keyPoint.x < d.x)
  193.         d.x = keyPoint.x;
  194.     if (keyPoint.x > d.x + size.x)
  195.         d.x = keyPoint.x - size.x;
  196.     if (keyPoint.y < d.y)
  197.         d.y = keyPoint.y - 1;        // fix Help-2
  198.     if (keyPoint.y > d.y + size.y)
  199.     d.y = keyPoint.y - size.y;
  200.     if ((d.x != delta.x) || (d.y != delta.y))
  201.      scrollTo(d.x, d.y);
  202. }
  203.  
  204. void THelpViewer::switchToTopic( int keyRef )
  205. {
  206. #ifdef HELPEXTENSIONS
  207.     if (hFile->helpAlreadyPopped)
  208.       oldSelected[THelpFile::oldCount] = selected;
  209.  
  210.     if (owner!=0)
  211.       owner->lock();
  212. #endif
  213.  
  214.     if (topic != 0)
  215.     delete topic;
  216.     topic = hFile->getTopic(keyRef);
  217.     topic->setWidth(size.x);
  218.     scrollTo(0, 0);
  219.     setLimit(limit.x, topic->numLines());
  220.     selected = 1;
  221.  
  222. #ifdef HELPEXTENSIONS
  223.     if ((keyRef == previousTopic) && (oldSelected[THelpFile::oldCount]!=0))
  224.     {
  225.         selected = oldSelected[THelpFile::oldCount];
  226.         drawView();
  227.         if (selected <= topic->getNumCrossRefs())
  228.         {
  229.         TPoint keyPoint;
  230.         uchar keyLength;
  231.         makeSelectVisible(selected-1,keyPoint,keyLength,keyRef);
  232.         }
  233.     }
  234.     else
  235.         drawView();
  236.  
  237.     if (owner!=0)
  238.         owner->unlock();
  239. #else
  240.       drawView();
  241. #endif
  242. }
  243.  
  244. void THelpViewer::handleEvent( TEvent& event )
  245. {
  246.  
  247.     TPoint keyPoint, mouse;
  248.     uchar keyLength;
  249.     int keyRef;
  250.     int keyCount;
  251.  
  252.  
  253.     TScroller::handleEvent(event);
  254.     switch (event.what)
  255.         {
  256.  
  257.         case evKeyDown:
  258.             switch (event.keyDown.keyCode)
  259.                 {
  260.                 case kbTab:
  261.                     ++selected;
  262.                     if (selected > topic->getNumCrossRefs())
  263.                         selected = 1;
  264.                     if ( topic->getNumCrossRefs() != 0 )
  265.                         makeSelectVisible(selected-1,keyPoint,keyLength,keyRef);
  266.                     break;
  267.                 case kbShiftTab:
  268.                     --selected;
  269.                     if (selected == 0)
  270.                         selected = topic->getNumCrossRefs();
  271.                     if ( topic->getNumCrossRefs() != 0 )
  272.                         makeSelectVisible(selected-1,keyPoint,keyLength,keyRef);
  273.                     break;
  274.                 case kbEnter:
  275.                     if (selected <= topic->getNumCrossRefs())
  276.                         {
  277.                         topic->getCrossRef(selected-1, keyPoint, keyLength, keyRef);
  278.                         switchToTopic(keyRef);
  279.                         }
  280.